﻿using CCWin;
using SuperMap.Data;
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MDF
{

    public partial class SJJZTY : CCSkinMain
    {
        /// <summary>
        /// 公开处理数据集的信息，以刷新地图窗口
        /// </summary>
        public string setInfo;
        private Dataset m_dataset;
        private Datasources m_datasources;
        private PrjCoordSys m_prjCoordSys;
        private PrjChange m_prjChange;

        /// <summary>
        /// 数据校正窗体初始化
        /// </summary>
        /// <param name="datasources">传入要处理的数据源集合</param>
        public SJJZTY(Datasources datasources)
        {
            InitializeComponent();
            //获取数据源
            m_datasources = datasources;
            //实例化
            m_prjCoordSys = new PrjCoordSys();
            m_prjChange = new PrjChange();

            setInfo = "";
            //填充控件
            FullComboBox();
        }

        /// <summary>
        /// 填充控件信息
        /// </summary>
        private void FullComboBox()
        {
            try
            {
                
                //源数据填充
                if (this.Start_Source_Combo.SelectedIndex == -1)
                {
                    for (int i = 0; i < m_datasources.Count; i++)
                    {
                        this.Start_Source_Combo.Items.Add(m_datasources[i].Alias);
                    }
                }
                //默认选择第一个数据源
                Datasets m_datasets = m_datasources[0].Datasets;
                if (this.Start_Source_Combo.SelectedIndex == -1)
                {
                    this.Start_Source_Combo.SelectedIndex = 0;
                }
                else if (this.Start_Source_Combo.SelectedIndex > 0)
                {
                    //更新为当前选择的数据源
                    m_datasets = m_datasources[this.Start_Source_Combo.SelectedIndex].Datasets;
                }
                //数据集填充
                if (m_datasets.Count > 0)
                {
                    for (int j = 0; j < m_datasets.Count; j++)
                    {
                        this.Start_Dataset_Combo.Items.Add(m_datasets[j].Name);
                    }
                    //默认选择第一个数据集
                    m_dataset = m_datasets[0];
                    this.Start_Dataset_Combo.SelectedIndex = 0;

                    //另存的数据集名称填充
                    this.Save_Dataset_Combo.Items.Add(m_dataset.Name);
                    this.Save_Dataset_Combo.SelectedIndex = 0;

                    //投影信息填充
                    this.StartCrood_TextBox.Text = m_prjChange.GetCoordPrjInfo(m_dataset.PrjCoordSys);
                }

                //另存数据填充
                for (int n = 0; n < m_datasources.Count; n++)
                {
                    //源数据已选，就不填充
                    if (n != this.Start_Source_Combo.SelectedIndex)
                    {
                        this.Save_Source_Combo.Items.Add(m_datasources[n].Alias);
                    }
                }

                //其他的填充
                this.ConMethod_Combo.SelectedIndex = 0;//默认第一种转换方法
                GetPrjXmlName();//填充坐标系名称

            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取并填充坐标系名称
        /// </summary>
        private void GetPrjXmlName()
        {
            try
            {
                string prjPath = Application.StartupPath + "\\Data\\Favorites";
                string[] files = Directory.GetFileSystemEntries(prjPath);

                foreach (string file in files)
                {
                    this.CoordSys_Combo.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file));
                }
                //this.CoordSys_Combo.Items.Add("更多...");
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 选择 源_数据源 时更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Start_Source_Combo_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                //清空控件
                this.Start_Dataset_Combo.Items.Clear();
                this.Save_Dataset_Combo.Items.Clear();
                this.Save_Source_Combo.Items.Clear();
                this.StartCrood_TextBox.Text = "";
                //重新填充一遍控件
                FullComboBox();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 选择 数据集 时更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Start_Dataset_Combo_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                //比较一下是否更新了选项
                if (!this.Start_Dataset_Combo.Text.Equals(this.Save_Dataset_Combo.Text))
                {
                    //更新数据集和投影信息
                    Datasets datasets = m_datasources[this.Start_Source_Combo.Text].Datasets;
                    m_dataset = datasets[this.Start_Dataset_Combo.Text];
                    this.StartCrood_TextBox.Text = m_prjChange.GetCoordPrjInfo(m_dataset.PrjCoordSys);
                    //先清除命名，再添加
                    this.Save_Dataset_Combo.Items.Clear();
                    this.Save_Dataset_Combo.Items.Add(m_dataset.Name);
                    this.Save_Dataset_Combo.SelectedIndex = 0;
                }
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        /// <summary>
        /// 选择 目标坐标系 时更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CoordSys_Combo_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                //将投影坐标系名称转为枚举中具体的类型（舍弃）
                //PrjCoordSys prjCoordSys = new PrjCoordSys((PrjCoordSysType)System.Enum.Parse(typeof(PrjCoordSysType), this.CoordSys_Combo.Text));

                if (!this.CoordSys_Combo.Text.Equals("更多..."))
                {
                    //从文件夹中获取投影数据
                    string prjName = this.CoordSys_Combo.Text;
                    string prjPath = Application.StartupPath + "\\Data\\Favorites\\" + prjName + ".xml";
                    bool su = m_prjCoordSys.FromFile(prjPath, PrjFileType.SuperMap);
                    this.EdtoCrood_TextBox.Text = m_prjChange.GetCoordPrjInfo(m_prjCoordSys);
                }
                else
                {
                    //更多投影坐标系（待完善）
                }

            }
            catch (Exception)
            {

                throw;
            }
            
        }
        ///// <summary>
        ///// 检查非空(舍弃)
        ///// </summary>
        //private bool IsNoEmpty( bool m_checked)
        //{
        //    bool result = true;
        //    if (m_checked)
        //    {
        //        foreach(Control control in this.Controls)
        //        {
        //            if(control is ComboBox)
        //            {
        //                if ((control as ComboBox).SelectedIndex == -1)
        //                {
        //                    string labName = (control as ComboBox).Name.Replace("Combo","lab");
        //                    Label label = (Label)this.Controls.Find(labName,true)[0];
        //                    MessageBox.Show("未选择" + label.Text, "注意");
        //                    result = false;
        //                    break;
        //                }
        //            }
        //        }
        //    }
        //    else
        //    {
        //        foreach (Control control in this.Controls)
        //        {
        //            if (control is ComboBox)
        //            {
        //                if ((control as ComboBox).Name != "Save_Source_Combo")
        //                {
        //                    if ((control as ComboBox).SelectedIndex == -1)
        //                    {
        //                        string labName = (control as ComboBox).Name.Replace("Combo", "lab");
        //                        Label label = (Label)this.Controls.Find(labName, true)[0];
        //                        MessageBox.Show("未选择" + label.Text, "注意");
        //                        result = false;
        //                        break;
        //                    }
        //                }
                        
        //            }
        //        }
        //    }
        //    return result;
        //}

        /// <summary>
        /// 确认转换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OK_Button_Click(object sender, EventArgs e)
        {
            try
            {
                //获取选中的各项参数设置（m_dataset、m_prjCoordSys有了）
                string ed_setrame = m_dataset.Name;
                Datasource ed_datasource;
                string transmethod = "GeocentricTransalation";
                if (!this.ConMethod_Combo.Text.Equals("更多..."))
                {
                    transmethod = this.ConMethod_Combo.Text.Replace("(3-para)", string.Empty).Replace(" ", string.Empty);
                }
                
                //检查参数设置是否合理
                if (this.CoordSys_Combo.SelectedIndex == -1)
                {
                    MessageBox.Show("请选择目标坐标系！", "警告");
                }
                else
                {
                    if (this.Save_check.Checked)
                    {
                        if (this.Save_Source_Combo.SelectedIndex == -1)
                        {
                            MessageBox.Show("请选择另存位置数据源！", "警告");
                        }
                        else
                        {
                            ed_datasource = m_datasources[this.Save_Source_Combo.Text];
                            string result = m_prjChange.TransformPrj(m_dataset, m_prjCoordSys, ed_datasource, ed_setrame, new CoordSysTransParameter(), transmethod);
                            setInfo = m_dataset.Name + "@" + m_dataset.Datasource.Alias;
                            MessageBox.Show(result,"提示：");
                            this.DialogResult = DialogResult.OK;
                        }
                    }
                    else
                    {
                        string result = m_prjChange.TransformPrj(m_dataset, m_prjCoordSys, new CoordSysTransParameter(), transmethod);
                        setInfo = m_dataset.Name + "@" + m_dataset.Datasource.Alias;
                        MessageBox.Show(result, "提示：");
                        this.DialogResult = DialogResult.OK;
                    }
                }
                
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 取消操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void No_Button_Click(object sender, EventArgs e)
        {
            try
            {
                //关闭窗体
                this.Close();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
